<!doctype html>
<html>

<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Pragma" content="no-cache" />
  <meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
  <meta http-equiv="Expires" content="0" />
</head>
<style>
  * {
    padding: 0;
    margin: 0;
  }

  html,
  body {
    overflow: hidden;
  }

  .plugin {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 10px;
    left: 30px;
  }
</style>

<body>
  <div>
    <div id="divPlugin" class="plugin"></div>
  </div>
</body>
<script src="../jquery-1.7.1.min.js"></script>
<script id="videonode" src="../codebase/webVideoCtrl.js"></script>
<script>

  // 创建适用于特定类型文件的URL
  function queryParams(key) {
    // 创建一个新的URL对象
    const url = new URL(window.location.href);
    // 使用URLSearchParams获取查询参数
    const queryParams = new URLSearchParams(url.search);
    // 获取特定的查询参数
    return queryParams.get(key); // 返回 'value1'
  }


  $(function () {

    let ip = queryParams('ip')
    let width = queryParams('width')
    let height = queryParams('height')
    let left = queryParams('left')
    let top = queryParams('top')
    let streamType = queryParams('streamType')
    // 检查插件是否已经安装过
    var iRet = WebVideoCtrl.I_CheckPluginInstall();
    if (-1 == iRet) {
      alert("您还未安装过插件，点击确定下载HCWebSDKPlugin.exe，下载完成后双击安装使用！");
      window.location.href = '../codebase/HCWebSDKPlugin.exe'
      return;
    }

    console.log('result:', ip);

    var oPlugin = {
      // iWidth: width ||  600,             // plugin width
      // iHeight: height || 400             // plugin height
    };

    $('#divPlugin').css({
      width: width + 'px',
      height: height + 'px',
      top: top + 'px',
      left: left + 'px',
      background: 'rgba(0,0,0,0)',
      border: 'none',
    })
    var oLiveView = {
      iProtocol: 1,            // protocol 1��http, 2:https
      szIP: ip,    // protocol ip
      szColorProperty: "plugin-background:rgba(0,0,0,0); sub-background:rgba(0,0,0,0); sub-border:rgba(0,0,0,0); sub-border-select:rgba(0,0,0,0)",   //2:PS 11:MP4
      szPort: "80",            // protocol port
      szUsername: "admin",     // device username
      szPassword: "HK888888", // device password
      iStreamType: Number(streamType || 1),          // stream 1��main stream  2��sub-stream  3��third stream  4��transcode stream
      iChannelID: 1,           // channel no
      bZeroChannel: false      // zero channel
    };

    // Init plugin parameters and insert the plugin
    WebVideoCtrl.I_InitPlugin({
      bWndFull: true, //Wether support doule clicking to switch the full-screen mode: it's supported by default; true:support, false:not support
      iWndowType: 1,
      szColorProperty: oLiveView.szColorProperty,
      cbSelWnd: function (xmlDoc) {
        g_iWndIndex = parseInt($(xmlDoc).find("SelectWnd").eq(0).text(), 10);
        var szInfo = "the selected window index: " + g_iWndIndex;
        showCBInfo(szInfo);
      },
      cbDoubleClickWnd: function (iWndIndex, bFullScreen) {
        var szInfo = "present window number to zoom: " + iWndIndex;
        if (!bFullScreen) {
          szInfo = "present window number to restore: " + iWndIndex;
        }
        showCBInfo(szInfo);
      },
      cbEvent: function (iEventType, iParam1, iParam2) {
        if (2 == iEventType) {
          showCBInfo("window " + iParam1 + "playback finished!");
        } else if (-1 == iEventType) {
          showCBInfo("device " + iParam1 + "network error!");
        } else if (3001 == iEventType) {
          clickStopRecord(g_szRecordType, iParam1);
        }
      },
      cbInitPluginComplete: function () {
        WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
          WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
            if (bFlag) {
              alert("Detect the latest version, please double click HCWebSDKPlugin.exe to update!");
            }
            WebVideoCtrl.I_Login(oLiveView.szIP, oLiveView.iProtocol, oLiveView.szPort, oLiveView.szUsername, oLiveView.szPassword, {
              timeout: 3000,
              success: function (xmlDoc) {
                console.log('result:登陆成功');
                var szDeviceIdentify = oLiveView.szIP + "_" + oLiveView.szPort;
                setTimeout(function () {
                  WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
                    iStreamType: oLiveView.iStreamType,
                    iChannelID: oLiveView.iChannelID,
                    bZeroChannel: oLiveView.bZeroChannel
                  });
                }, 10);
              },
              error: function (oError) {
                if (ERROR_CODE_LOGIN_REPEATLOGIN === status) {
                  showOPInfo(szDeviceIdentify + " is already login");
                } else {
                  showOPInfo(szDeviceIdentify + " login failed", oError.errorCode, oError.errorMsg);
                }
              }
            });
          });
        }, () => {
          alert("The plugin initialization failed. Please confirm if the plugin has been installed; If not installed, please double click on HCWebSDKPlugin.exe to install it!");
        });
      }
    });


    // 初始化插件参数及插入插件
    // WebVideoCtrl.I_InitPlugin(oPlugin.iWidth, oPlugin.iHeight, {
    //     bWndFull: true,//是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
    //     iWndowType: 1,

    //     szColorProperty:oLiveView.szColorProperty,
    //     cbInitPluginComplete: function () {
    //         WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");

    //         // 检查插件是否最新
    //         if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
    //             alert("检测到新的插件版本，点击确定下载WebComponentsKit.exe，双击WebComponentsKit.exe升级！");
    //             window.location.href = '../codebase/WebComponentsKit.exe'
    //             return;
    //         }

    //         // 登录设备
    //         WebVideoCtrl.I_Login(oLiveView.szIP, oLiveView.iProtocol, oLiveView.szPort, oLiveView.szUsername, oLiveView.szPassword, {
    //             success: function (xmlDoc) {
    //                 // 开始预览
    //                 var szDeviceIdentify = oLiveView.szIP + "_" + oLiveView.szPort;
    //                 setTimeout(function () {
    //                     WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
    //                         iStreamType: oLiveView.iStreamType,
    //                         iChannelID: oLiveView.iChannelID,
    //                         bZeroChannel: oLiveView.bZeroChannel
    //                     });
    //                 }, 10);
    //             }
    //         });
    //     }
    // });

    // 关闭浏览器
    $(window).unload(function () {
      WebVideoCtrl.I_Stop();
      var szDeviceIdentify = oLiveView.szIP + "_" + oLiveView.szPort;
      // WebVideoCtrl.I_Logout(szDeviceIdentify).then(() => {
      //     alert('退出成功！')
      // }, () => {
      //     alert('退出失败！')
      // });
    });
  });

</script>

</html>